package com.wxhandle.cleandemo.api.exception;

import javax.servlet.http.HttpServletRequest;

import com.wxhandle.cleandemo.api.exception.DomainException.ErrorCode;

import org.hibernate.exception.ConstraintViolationException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import lombok.extern.slf4j.Slf4j;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
	
	/**
	 * 处理自定义的业务异常
	 * @param req
	 * @param e
	 * @return
	 */
    @ExceptionHandler(value = DomainException.class)  
    @ResponseBody  
	public  ResponseEntity<ErrorResult> DomainExceptionHandler(HttpServletRequest req, DomainException e){
    	log.error("发生业务异常！原因是：{}",e.getMsg());
        return new ResponseEntity<ErrorResult>(
            ErrorResult.ofException(e), 
            HttpStatus.INTERNAL_SERVER_ERROR);
    }
	/**
	 * 处理GET输入验证的异常
	 * @param req
	 * @param e
	 * @return
	 */
    @ExceptionHandler(value = ConstraintViolationException.class)  
    @ResponseBody  
	public  ResponseEntity<ErrorResult> ConstraintViolationExceptionHandler(HttpServletRequest req, ConstraintViolationException e){
    	log.error("发生验证异常！原因是：{}",e.getMessage());
        return new ResponseEntity<ErrorResult>(
            ErrorResult.ofErrorCode(ErrorCode.DATA_VALID_FAIL, e.getMessage()), 
            HttpStatus.INTERNAL_SERVER_ERROR);
    }
	/**
	 * 处理GET输入验证的异常
	 * @param req
	 * @param e
	 * @return
	 */
    @ExceptionHandler(value = DataIntegrityViolationException.class)  
    @ResponseBody  
	public  ResponseEntity<ErrorResult> DataIntegrityViolationExceptionHandler(HttpServletRequest req, DataIntegrityViolationException e){
    	log.error("发生数据关联异常！原因是：{}",e.getMessage());
        return new ResponseEntity<ErrorResult>(
            ErrorResult.ofErrorCode(ErrorCode.DB_DATA_Integrity, e.getMessage()), 
            HttpStatus.INTERNAL_SERVER_ERROR);
    }
	/**
	 * 处理POST输入验证的异常
	 * @param req
	 * @param e
	 * @return
	 */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)  
    @ResponseBody  
	public ResponseEntity<ErrorResult> MethodArgumentNotValidExceptionHandler(HttpServletRequest req, MethodArgumentNotValidException e){
    	log.error("发生验证异常！原因是：{}",e.getMessage());
        return new ResponseEntity<ErrorResult>(
            ErrorResult.ofErrorCode(ErrorCode.DATA_VALID_FAIL, e.getMessage()), 
            HttpStatus.INTERNAL_SERVER_ERROR);
    }

	/**
	 * 处理空指针的异常
	 * @param req
	 * @param e
	 * @return
	 */
	@ExceptionHandler(value =NullPointerException.class)
	@ResponseBody
	public ResponseEntity<ErrorResult> exceptionHandler(HttpServletRequest req, NullPointerException e){
		log.error("发生空指针异常！原因是:",e);
        return new ResponseEntity<ErrorResult>(
            ErrorResult.ofErrorCode(ErrorCode.OBJECT_NOT_NULL, e.getMessage()), 
            HttpStatus.INTERNAL_SERVER_ERROR);
	}

    /**
        * 处理其他异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value =Exception.class)
	@ResponseBody
	public ResponseEntity<ErrorResult> exceptionHandler(HttpServletRequest req, Exception e){
    	log.error("未知异常！原因是:",e);
       	return new ResponseEntity<ErrorResult>(
               ErrorResult.ofErrorCode(ErrorCode.INTERNAL_SERVER_ERROR, e.getMessage()), 
               HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
